//
//  XCHomeController.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/5.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit

private let cellId = "homeCell"

class XCHomeController: XCBasicTableViewController {
    
    lazy var statusModelList : [status] = [status]()
    
    //懒加载下拉刷新的小菊花
    lazy var myRefresh : UIRefreshControl = {
        
        let refresh = UIRefreshControl(frame: CGRectMake(0, -60, screenWith, 60))
        
        refresh.backgroundColor = UIColor.blackColor()
        
        refresh.addTarget(self, action: "refreshing", forControlEvents: .ValueChanged)
        
        return refresh
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !UserCount.shareAcount.userLoginFlage {
            
            return
        }
        
        //注册cell
        
        prepareCell()
        
        //        status.loadHomeData { (array) -> () in
        //
        //
        //            guard let arr = array else{
        //
        //                print("数组有错")
        //                return
        //            }
        //
        ////            print("arr=\(arr.count)")
        //            self.statusModelList = arr
        //
        //            self.tableView.reloadData()
        //    }
        
        refreshControl = myRefresh
        
        loadData()
    }
    
    
    //加载数据
    private func loadData (){
        

        
        //定义 since_id 和 max_id
        var since_id : Int64 = 0
        var max_id : Int64 = 0
        
        
        if indicatior.isAnimating() {
        
            //上啦加载更多
            max_id = statusModelList.last?.id ?? 0

            
        }else {
        
          //下拉刷新
            //取得当前最新微博的id  然后返回更新id的微博
            since_id = statusModelList.first?.id ?? 0

        }
        
        
        status.loadHomeData(since_id, max_id: max_id) { (array) -> () in
            self.myRefresh.endRefreshing()

            guard let dict = array else {
                
                print("字典获取失败")
                return
            }
            
            
            if since_id > 0 {
            
                self.statusModelList = dict + self.statusModelList
            
            }else{
                
                self.indicatior.stopAnimating()
                self.statusModelList += dict
            }
            
            self.tableView.reloadData()
            
            
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    private func prepareCell(){
        
        //注册cell
        
        tableView.registerClass(statusCell.self, forCellReuseIdentifier: cellId)
        
        //        //设置预估行
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        
        //        tableView.rowHeight = 400
        
        //        tableView.bounces = false
        
        
        //定义一个下拉刷新控件
        
        
        //指定footerView指示器
        tableView.tableFooterView = indicatior
        
//        indicatior.startAnimating()
        
    }
    
    //刷新数据的方法
    func refreshing (){
        
        //        print("refreshing")
        
        loadData()
        
    }
    
    
    // MARK: - Table view data source
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        
        //        print(statusModelList.count)
        return statusModelList.count
        
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell  = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! statusCell
        
        
        
        //在下拉的时候开始刷新
        if indexPath.row == statusModelList.count - 1 && indicatior.isAnimating() {
            
            //小菊花开始转动
            indicatior.startAnimating()
            
            loadData()
        }
        
        let model = statusModelList[indexPath.row]
        cell.model = model
        //        cell.textLabel?.text = statusModelList[indexPath.row].text
        //        
        //        print(cell.textLabel?.text)
        
        return cell
    }
    
    
    
    //懒加载指示器
    let indicatior : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
    
    
}
